//
//  CandleStickChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "CandleStickChartViewController.h"
@interface CandleStickChartViewController ()
@end

@implementation CandleStickChartViewController

CandleStickChartView *sampleCandleStickChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initChartView{
    sampleCandleStickChartView=[[CandleStickChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleCandleStickChartView.backgroundColor = [UIColor whiteColor];
    sampleCandleStickChartView.delegate = self;
    [self.view addSubview:sampleCandleStickChartView];
    [self initChartStyleAndData];
}


-(void)initChartStyleAndData{
    [self setupChartView:sampleCandleStickChartView];
    [self setChartParam:sampleCandleStickChartView];
    [self setLegendStyle:sampleCandleStickChartView];
    [self initChartData];
    [sampleCandleStickChartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
}

- (void)setupChartView:(CandleStickChartView *)chartView
{
    chartView.descriptionText = @"";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.maxVisibleValueCount = 60;
    chartView.pinchZoomEnabled = NO;
    chartView.drawGridBackgroundEnabled = NO;
}

-(void)setChartParam:(CandleStickChartView *)chartView{
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.spaceBetweenLabels = 2.0;
    xAxis.drawGridLinesEnabled = NO;
    
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.labelCount = 7;
    leftAxis.drawGridLinesEnabled = NO;
    leftAxis.drawAxisLineEnabled = NO;
    
    ChartYAxis *rightAxis = chartView.rightAxis;
    rightAxis.enabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(CandleStickChartView *)chartView{
    chartView.legend.enabled = NO;
}

- (void)initChartData
{
    [self setDataCount:39 range:100 chart:sampleCandleStickChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(CandleStickChartView *)chartView
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i + 1990) stringValue]];
    }
    
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double mult = (range + 1);
        double val = (double) (arc4random_uniform(40)) + mult;
        double high = (double) (arc4random_uniform(9)) + 8.0;
        double low = (double) (arc4random_uniform(9)) + 8.0;
        double open = (double) (arc4random_uniform(6)) + 1.0;
        double close = (double) (arc4random_uniform(6)) + 1.0;
        BOOL even = i % 2 == 0;
        [yVals1 addObject:[[CandleChartDataEntry alloc] initWithXIndex:i shadowH:val + high shadowL:val - low open:even ? val + open : val - open close:even ? val - close : val + close]];
    }
    
    CandleChartDataSet *set1 = [[CandleChartDataSet alloc] initWithYVals:yVals1 label:@"Data Set"];
    set1.axisDependency = AxisDependencyLeft;
    [set1 setColor:[UIColor colorWithWhite:80/255.f alpha:1.f]];
    
    set1.shadowColor = UIColor.darkGrayColor;
    set1.shadowWidth = 0.7;
    set1.decreasingColor = UIColor.redColor;
    set1.decreasingFilled = YES;
    set1.increasingColor = [UIColor colorWithRed:122/255.f green:242/255.f blue:84/255.f alpha:1.f];
    set1.increasingFilled = NO;
    set1.neutralColor = UIColor.blueColor;
    
    CandleChartData *data = [[CandleChartData alloc] initWithXVals:xVals dataSet:set1];
    
    chartView.data = data;
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected, stack-index %ld", (long)highlight.stackIndex);
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end